<template>
  <div class="item1">
    <div class="breadcrumb" :gutter="24">
      <el-form ref="form" label-width="70px" size="small">
        <!-- <el-row :gutter="28"> -->
        <div class="btngroup" style="padding-right:15px;float:right">
          <el-button type="primary" size="small" @click="handleAdd()">{{ $t("common.add") }}</el-button>
          <el-button type="primary" size="small" @click="handleEdit()">{{ $t("common.edit") }}</el-button>
          <el-button type="primary" size="small" @click="handleDel()">{{ $t("common.remove") }}</el-button>
        </div>
        <!-- </el-row> -->
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="tableAuto">
      <el-table :data="tableData" style="width:100%" :height="height" @row-click="rowClick" border stripe
        :header-cell-style="{background:'#EAEBEC'}" v-loading="tableLoading" ref="refTable" highlight-current-row @sort-change='sortChange'>
        <el-table-column label="" width="50" align="center">
          <template slot-scope="scope">
            <div>
              <el-radio :label="scope.row.fid" v-model="radioId">&nbsp;</el-radio>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="index" :label="$t('common.number')" width="90" align="center">
          <template slot-scope="scope">
            {{table_index(scope.$index+1)}}
          </template>
        </el-table-column>
        <el-table-column prop="name" width="150" show-overflow-tooltip :label="$t('basic.lable_name')" sortable='custom'></el-table-column>
        <el-table-column prop="type" width="100" show-overflow-tooltip :label="$t('basic.data_type')">
          <template slot-scope="scope">
            {{getDataType(scope.row.type)}}
          </template>
        </el-table-column>
        <el-table-column prop="wr" width="90" show-overflow-tooltip :label="$t('basic.read_write')">
          <template slot-scope="scope">
            {{getwrName(scope.row.wr)}}
          </template>
        </el-table-column>
        <el-table-column prop="uom" width="100" show-overflow-tooltip :label="$t('basic.company')"></el-table-column>
        <!-- <el-table-column prop="hi" width="70" show-overflow-tooltip label="hi"></el-table-column>
        <el-table-column prop="hihi" width="70" show-overflow-tooltip label="hihi"></el-table-column>
        <el-table-column prop="lo" width="70" show-overflow-tooltip label="lo"></el-table-column>
        <el-table-column prop="lolo" width="70" show-overflow-tooltip label="lolo"></el-table-column> -->

        <el-table-column prop="isCommu" :label="$t('basic.device_commu')" width="100">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.isCommu" disabled />
          </template>
        </el-table-column>
        <el-table-column prop="defaultVal" width="100" show-overflow-tooltip :label="$t('basic.default_val')"></el-table-column>
        <el-table-column prop="description" show-overflow-tooltip :label="$t('common.describe')"></el-table-column>
      </el-table>
    </div>

    <div class="pagination-area">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[20, 30, 50, 100]"
        :page-size="pager.pageSize" :current-page="pager.pageNum" layout="total, sizes, prev, pager, next, jumper" :total="pager.total">
      </el-pagination>
    </div>

    <el-dialog v-dialogDrag :title="title" :visible.sync="isShowDialog" :close-on-click-modal="false"
      :width="screenWidth > 1024 ? '30%' : '50%'" class="isShowDialog" :before-close="handleClose">
      <el-form :model="ruleForm" label-width="85px" ref="ruleForm" size="small" :rules="rules" :hide-required-asterisk="true">
        <el-row :gutter="35">
          <el-col :xs="24">
            <el-form-item :label="$t('dialog.equip_cls')" prop="equipmentClassId">
              <el-select v-model="ruleForm.equipmentClassId" disabled placeholder="请选择">
                <el-option v-for="item in equipmentClassList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('dialog.name')" prop="name">
              <el-input v-model="ruleForm.name" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('basic.data_type')" prop="type" class="selectClass">
              <el-select v-model="ruleForm.type" placeholder="" clearabl>
                <el-option v-for="(item,index) in dataTypeList" :key="index" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :xs="24">
            <el-form-item :label="$t('basic.company')" prop="uom">
              <el-select v-model="ruleForm.uom" placeholder="" clearable>
                <el-option v-for="(item,index) in uomIdList" :key="index" :label="item.fid" :value="item.fid"></el-option>
              </el-select>
              <!-- <el-input v-model="ruleForm.uom" placeholder="" clearable></el-input> -->
            </el-form-item>
          </el-col>

          <el-col :xs="24">
            <el-form-item :label="$t('basic.device_commu')" prop="isCommu">
              <el-switch v-model="ruleForm.isCommu" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" v-if="ruleForm.isCommu">
            <el-form-item :label="$t('basic.read_write')" prop="wr" class="selectClass">
              <el-select v-model="ruleForm.wr" placeholder="" clearable>
                <el-option v-for="(item,index) in wrList" :key="index" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24">
            <el-form-item :label="$t('basic.default_val')" prop="name">
              <el-input v-model="ruleForm.defaultVal" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>

          <!-- <el-col :xs="24">
                      <el-form-item label="标签" prop="tag">
                        <el-input v-model="ruleForm.tag" placeholder="" clearable></el-input>
                      </el-form-item>
                    </el-col> -->

          <el-col :xs="24">
            <el-form-item :label="$t('common.describe')" prop="description">
              <el-input type="textarea" v-model="ruleForm.description" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="handleCreateMaterial('ruleForm')"
          :loading="loading">{{ $t("common.preservation") }}</el-button>
        <el-button @click="handleClose()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { configure } from "@/api/configure"
export default {
  props: {
    parentFid: String,
    equipmentClassList: Array
  },
  data () {
    return {
      screenWidth: document.body.clientWidth,
      height: 32 * 11.3,
      ruleForm: {
        equipmentClassId: '',
        name: "",
        wr: "",
        uom: "",
        type: "",
        tag: "",
        description: "",
        defaultVal: "",
        isCommu: false,
      },
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '数据类型不能为空', trigger: 'blur' }],
        tag: [{ required: true, message: '标签不能为空', trigger: 'blur' }],
      },
      dataTypeList: [{
        label: "Float",
        value: 0
      },
      //  {
      //   label: "Int",
      //   value: 1
      // }, 
      {
        label: "String",
        value: 2
      },
        // {
        //   label: "Bool",
        //   value: 3
        // }
      ],
      isShowDialog: false,
      title: this.$t('dialog.equip_cls_add_argu'),
      loading: false,
      tableData: [],
      tableLoading: false,
      radioId: "",
      pager: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
        field: '',
        order: ''
      },
      cardIndex: -1,
      icon: "icon-biaoge",
      currObj: {},
      pagerClass: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      equipmentClassName: "",
      uomIdList: [],
      wrList: [{
        label: "读写",
        value: 0
      }, {
        label: "只读",
        value: 1
      }, {
        label: "只写",
        value: 2
      }]
    }
  },
  created () {
    this.getUomList()
  },
  watch: {
    screenWidth (val) {
      this.screenWidth = Number(val)
    },
    parentFid (val) {
      if (val) {
        this.$set(this.ruleForm, 'equipmentClassId', val)
        this.getEquipmentClassTagDefineList()
      }
    }
  },
  mounted () {
    const that = this
    window.addEventListener('resize', function () {
      that.screenWidth = document.body.offsetWidth;
    })
  },
  methods: {
    handleSearch () {
      this.pager.pageNum = 1;
      this.getEquipmentClassTagDefineList()
    },
    getEquipmentClassTagDefineList () {
      const param = {
        page: this.pager.pageNum,
        size: this.pager.pageSize,
        field: this.pager.field,
        order: this.pager.order,
        equipmentClassId: this.parentFid
      }

      this.tableLoading = true
      configure.getEquipmentClassTagDefineList(param).then(res => {
        this.tableLoading = false
        if (res.code == 200) {
          this.tableData = res.data.rows
          this.pager.total = res.data.total
        }
      }).catch(e => {
        this.tableLoading = false
        this.$message.error('服务器繁忙,请稍后再试');
      })
    },
    sortChange (column) {
      if (column.order === 'descending') {
        this.pager.field = column.prop
        this.pager.order = 'DESC'
      } else {
        this.pager.field = column.prop
        this.pager.order = 'ASC'
      }
      this.pager.pageNum = 1
      this.getEquipmentClassTagDefineList()
    },
    handleSizeChange (val) {
      this.pager.pageNum = 1
      this.pager.pageSize = val
      this.getEquipmentClassTagDefineList()
    },
    handleCurrentChange (val) {
      this.pager.pageNum = val;
      this.getEquipmentClassTagDefineList()
    },
    handleClose () {
      this.isShowDialog = false
    },
    handleAdd () {
      if (this.parentFid === '') {
        this.$message.error('请选择设备类型');
        return
      }
      this.title = this.$t('dialog.equip_cls_add_argu')
      try {
        let copyRow = this.tableData.find(item => item.fid == this.radioId)
        this.$refs['ruleForm'].resetFields();
        this.ruleForm = {
          fid: "",
          equipmentClassId: copyRow.equipmentClassId,
          name: "",
          wr: "",
          uom: "",
          type: "",
          tag: "",
          description: "",
          defaultVal: "",
          isCommu: false,
        }
      } catch (e) { }
      this.isShowDialog = true
    },
    handleEdit () {
      if (this.radioId === '') {
        this.$message.error(this.$t('dialog.please_sel_edit'));
        return
      }
      this.title = this.$t('dialog.equip_cls_edit_argu')
      let copyRow = this.tableData.find(item => item.fid == this.radioId)
      this.ruleForm = {
        fid: copyRow.fid,
        equipmentClassId: copyRow.equipmentClassId,
        name: copyRow.name,
        wr: copyRow.wr,
        uom: copyRow.uom,
        type: copyRow.type,
        tag: copyRow.tag,
        description: copyRow.description,
        isCommu: copyRow.isCommu == 0 ? false : true,
        defaultVal: copyRow.defaultVal,
      }
      this.isShowDialog = true
    },
    handleCreateMaterial (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = Object.assign({}, this.ruleForm)

          // param.hihiEnable = this.ruleForm.hihiEnable === false ? 0 : 1,
          //   param.hiEnable = this.ruleForm.hiEnable === false ? 0 : 1,
          //   param.loEnable = this.ruleForm.loEnable === false ? 0 : 1,
          //   param.loloEnable = this.ruleForm.loloEnable === false ? 0 : 1,
          this.loading = true
          configure.equipmentClassTagDefineAdd(param, this.title).then(res => {
            this.loading = false
            if (res.code == 200) {
              this.$refs[formName].resetFields();
              this.getEquipmentClassTagDefineList()
              this.isShowDialog = false
            } else {
              this.$message.error(res.msg);
            }
          }).catch(e => {
            this.loading = false
            this.$message.error('服务器繁忙,请稍后再试');
          })
        } else {
          return false
        }
      })
    },
    handleDel () {
      if (this.radioId === '') {
        this.$message.error(this.$t('dialog.please_sel_del'));
        return
      }
      let that = this
      this.$customConfirm({
        content: "请确认是否删除单元参数" + this.currObj.name,
        onok () {
          configure.removeEquipmentClassTagDefine(that.radioId).then(res1 => {
            if (res1.code == 200) {
              that.radioId = ""
              that.cardIndex = -1
              that.getEquipmentClassTagDefineList()
              that.$message({ duration: 1000, type: 'success', message: this.$t('common.del_succ') });
            } else {
              that.$message.error(res1.msg);
            }
          })
        }
      });
    },
    rowClick (row) {
      this.radioId = row.fid;
      this.currObj = row;
      this.$refs.refTable.setCurrentRow(row);
    },
    handleIndex (index, row) {
      this.cardIndex = index
      this.radioId = row.fid;
    },
    changePattern () {
      this.radioId = ''
      this.cardIndex = -1
    },
    // 获取计量单位列表
    getUomList () {
      const param = {
        page: 1,
        size: 9999
      }
      configure.getUomList(param).then(res => {
        if (res.code == 200) {
          this.uomIdList = res.data.rows
        }
      }).catch(e => {

      })
    },
    table_index (index) {
      return (this.pager.pageNum - 1) * this.pager.pageSize + index
    },
    getDataType (id) {
      if (!id && id !== 0) return ''
      return this.dataTypeList.find(item => item.value == id).label
    },
    getwrName (id) {
      if (!id && id !== 0) return ''
      return this.wrList.find(item => item.value == id).label

    }
  }
}
</script>

<style scoped>
.btngroup {
  float: left;
}
.change >>> .iconfont {
  font-size: 13px !important;
}
.titmepicker >>> .el-date-editor {
  width: 100%;
}
.titmepicker >>> .el-date-editor .el-range-separator {
  width: 8%;
}
.isShowDialog >>> .el-select {
  width: 100%;
}
.tableAuto,
.cardBox {
  clear: both;
}
.pagination-area {
  padding: 10px;
  border: 1px solid #eaebec;
  border-top: none;
}
.Desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cardBox {
  overflow-y: auto;
}
.card {
  background: #eaebec;
  margin-bottom: 20px;
  padding: 10px;
  border-radius: 3px;
}
.card p {
  font-size: 12px;
  line-height: 22px;
  transform: scale(0.9);
}
.desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.title {
  font-weight: bold;
}
.selectCard {
  background: #f6fbff;
}
@media only screen and (min-width: 1200px) {
  .el-col-lg-5 {
    width: 19.83333%;
  }
}
</style>